package com.Xht.www.servlet;

//后端响应获取用户账单流水的余额

import com.Xht.www.dao.WalletTransactionLogDAO;
import com.Xht.www.po.WalletTransactionLogPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/getWalletTransactionLog")
public class GetWalletTransactionLogServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int userId = Integer.parseInt(request.getParameter("userId"));
        WalletTransactionLogDAO dao = new WalletTransactionLogDAO();
        List<WalletTransactionLogPO> transactionLog = dao.getTransactionLogByUserId(userId);

        StringBuilder json = new StringBuilder("[");
        for (int i = 0; i < transactionLog.size(); i++) {
            WalletTransactionLogPO log = transactionLog.get(i);
            json.append("{");
            json.append("\"transactionTime\": \"").append(log.getTransactionTime()).append("\", ");
            json.append("\"transactionType\": \"").append(log.getTransactionType()).append("\", ");
            json.append("\"transactionAmount\": \"").append(log.getTransactionAmount()).append("\"");
            json.append("}");
            if (i < transactionLog.size() - 1) {
                json.append(",");
            }
        }
        json.append("]");

        out.print(json.toString());
        out.flush();
        out.close();
    }
}
